﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using LevenBlog.Core.Entities;
using LevenBlog.Core.Service;
using LevenBlog.Core.Utils;
using LevenBlog.Core.User;
using LevenBlog.Core.WebService.Entities;

namespace LevenBlog.WebService
{
    /// <summary>
    /// Summary description for Membership
    /// </summary>
    [WebService(Namespace = "http://leven.com.cn/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Membership : LevenBlog.Core.WebService.BaseService
    {
        [WebMethod]
        public User GetUser(string username, string password)
        {
            User result = null;
            if (Membership.Login(username, password))
            {
                result = new User();
                result.FillWithDbEntity(Membership.User);
            }
            return result;
        }

        [WebMethod]
        public FullUser GetFullUser(string username, string password)
        {
            FullUser fullUser = null;
            if (Membership.Login(username, password))
            {
                fullUser = new FullUser();
                Membership.LoadFull();
                fullUser.FillWithDbEntity(Membership.User, Membership.User.userinfo);
            }
            return fullUser;
        }

        [WebMethod]
        public Role GetRole(int roleid)
        {
            lb_usertype type = UserType.Get(Provider, roleid);
            if (type == null)
                return null;
            Role role = new Role();
            role.FillWithDbEntity(type);
            return role;
        }

        //[WebMethod]
        //public string GetCookieString(string username, string password)
        //{
        //    if (Membership.Login(username, password))
        //        return SecurityCookie.EncryptToBase64(Membership.User);
        //    else
        //        return null;
        //}

        [WebMethod]
        public User GetUserFromCookie(string cookieString)
        {
            try
            {
                lb_user user = SecurityCookie.DecryptFromBase64(cookieString);
                User result = new User();
                result.FillWithDbEntity(user);
                return result;
            }
            catch
            {
                return null;
            }
        }

        [WebMethod]
        public string GetSystemCookieName()
        {
            return UserMembership.cookieName;
        }
    }
}
